/*
 * @Author: TTangZH <your.email@example.com>
 * @Date: 2025-05-31
 * @Description: AS5600磁编码器接口
 */
#include "as5600.h"

uint8_t read_one_byte(uint8_t reg_addr)
{
    uint8_t data = 0;
    I2C_SendData(I2C2, reg_addr);
    while(RESET == I2C_GetFlagStatus(I2C2, I2C_STATUS_FLAG_TFE))
    {
    }
    I2C_ReadCmd(I2C2);
    while (RESET == I2C_GetFlagStatus(I2C2, I2C_STATUS_FLAG_RFNE))
    {
    }
    data = I2C_ReceiveData(I2C2);
    return data;
}

byte8_to_byte16 buff;
uint16_t read_raw_angle(void)
{    
    buff.data8[1] = read_one_byte(0x0C);
    buff.data8[0] = read_one_byte(0x0D);
    return buff.data16;
}

uint8_t detect_magnet(void)
{
    uint8_t ret_val;
    ret_val = read_one_byte(_stat);
    if(ret_val & 0x20)
    {
        ret_val = 1;
    }else
    {
        ret_val = 0;
    }    
    return ret_val;
}